package com.dule.Medic.pages;

import java.util.List;

import org.apache.tapestry5.annotations.CleanupRender;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.dule.Medic.beans.PatientBean;
import com.dule.Medic.model.Doctor;
import com.dule.Medic.model.Patient;
import com.dule.Medic.service.DoctorService;
import com.dule.Medic.service.PatientService;


public class ListPatients 
{
	@Property      
	@SessionState      
	private Doctor doctor;    
	private boolean doctorExists;          
	
	@Property
	private Patient patient;
	
	@SuppressWarnings("unused")
	@Property
	@Persist("flash")
	private String message; 
	
	@InjectComponent
	@Property
	private Zone PatientsList;
	
	@Inject       
	private DoctorService doctorservice; 
	@Inject       
	private PatientService patientservice;    
	@InjectPage
	private ExaminePatient examinePatient;
	
	@SetupRender   
	public void setUp()
	{        
		patient = new PatientBean();       
	}
	
	public Object onActivate()
	{      
		if(doctorExists)
		{                
			return null;      
		}          
		return FirstPage.class;   
	}      
	
	public List<Patient> getPatients()
    {
        return patientservice.retrieveAll();
    }
	
	@OnEvent(component="deletePatient")
    public Object Delete(Long Id)
    {
		patient=patientservice.retrieveOne(Id);
		patient.getMedEntries().size();	
		if(doctor.containsME(patient.getMedEntries()))
		{
			patientservice.deletePatient(patient);
			doctor=doctorservice.retrive(doctor);
			doctor.getMedEntries().size();
		}
		else message="Patient "+patient.getFirstName()+" "+patient.getLastName()+" cannot be deleted because \n " +
				"there are enteries from other doctor!!!";	
		return PatientsList.getBody();
    } 
	
	@OnEvent(component="examinepatient")
    public Object OnExaminePatient(Long Id)
    {
		patient=patientservice.retrieveOne(Id);
		patient.getMedEntries().size();
		examinePatient.SetPatient(patient);
		return examinePatient;
    } 
	
	@CleanupRender     
	public void cleanUp()
	{             
		message =null;        
	}  
	

}
